<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      function downloadSVG(svgString, fileName = "download-svg.svg") {
        // 1 创建 blob 对象
        const blob = new Blob([svgString], { type: "image/svg+xml" });
        // 2 生成下载链接
        const url = URL.createObjectURL(blob);
        // 3 下载
        const a = document.createElement("a");
        a.href = url;
        a.download = fileName;
        document.body.appendChild(a);
        a.click();
        document.body.removeChild(a);
        URL.revokeObjectURL(url);
      }

      // test
      const svgStr = `<svg xmlns="http://www.w3.org/2000/svg" version="1.1"><circle cx="100" cy="50" r="40" stroke="black"  stroke-width="2" fill="red" /></svg>`;
      downloadSVG(svgStr, "myImg.svg");
    </script>
  </body>
</html>
